package org.example.framework.generator;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.zaxxer.hikari.HikariDataSource;

import java.util.Collections;

/**
 * @author tangxiao
 */
public class GeneratorApplication {

    public static void main(String[] args) {
        String outPutDirPrefix = "/Users/tangxiao/work/eclipse-workspace/school-demo/oa-micro-boot/src/main/";

//        String url = "jdbc:postgresql://211.149.140.244:5444/sbhdb_pt?useUnicode=true";
        String url = "jdbc:sqlite:/Users/tangxiao/work/eclipse-workspace/school-demo/oa-micro-boot/src/main/resources/oa-sqlite.db";
//        String username = "sbhptDB";
//        String password = "ono";
        String username = "";
        String password = "";
        String pkg = "org.example.oa";
        String moduleName = "activity";
        String tableName = "activity_apply";
//        String schema = "fin_oa";
        String schema = "";
        String author = "tangxiao";
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setSchema(schema);
//        dataSource.setDriverClassName("org.postgresql.Driver");
        dataSource.setDriverClassName("org.sqlite.JDBC");
        dataSource.setJdbcUrl(url);
        FastAutoGenerator.create(new DataSourceConfig.Builder(dataSource))
                .globalConfig(builder -> {
                    builder.author(author)
                            // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(outPutDirPrefix+"java"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent(pkg) // 设置父包名
                            .moduleName(moduleName) // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, outPutDirPrefix+"resources")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tableName) // 设置需要生成的表名
                            .addTablePrefix(); // 设置过滤表前缀
                })
                .templateConfig((builder) -> {
                    builder.disable(TemplateType.ENTITY)
                            .entity("tpl/entity.java")

                            .service("tpl/service.java")
                            .serviceImpl("tpl/serviceImpl.java")
                            .mapper("tpl/mapper.java")
                            .mapperXml("tpl/mapper.xml")
                            .controller("tpl/controller.java");
                })
                //使用Freemarker引擎模板，默认的是Velocity引擎模板
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();

    }
}
